﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Team4Mock.App_Code
{
    public class CaptchaImage
    {
        public static Image GenericImage(out string _text, int _width, int _height)
        {
            Bitmap bmp = new Bitmap(_width, _height);
            Graphics g = Graphics.FromImage(bmp);
            Random rnd = new Random();

            // Fill in the background.        
            Rectangle rect = new Rectangle(0, 0, _width, _height);
            HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.Black, Color.Gray);
            g.FillRectangle(hatchBrush, rect);

            g.DrawLine(new Pen(Color.Blue, 1), new Point(0, _height / rnd.Next(2, 4)), new Point(_width, _height / rnd.Next(2, 6)));

            // Get random text
            string text = "abcdefghijklmnopqrstuvxy";
            string s = "";
            for (int i = 0; i < 5; i++)
            {
                s += text[rnd.Next(0, text.Length - 1)];
            }
            _text = s;


            Font f = new Font("Comic Sans MS", 20, FontStyle.Regular);
            Point p = new Point(-2, -7);
            g.DrawString(s, f, Brushes.White, p);

            g.DrawLine(new Pen(Color.Red, 1), new Point(0, _height / rnd.Next(2, 4)), new Point(_width, _height / rnd.Next(2, 6)));

            return bmp;
        }
    }
}